<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Hospital;

/**
 * 消息管理
 *
 * @icon fa fa-circle-o
 */
class News extends Backend
{

    /**
     * Warranty模型对象
     * @var \app\common\model\Warranty
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\News;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $this->Ids=$this->auth->getGroupIds();
            $where1="1=1";
            if($this->Ids[0]==2) {
                $admin = Admin::get($this->user["id"]);
                if ($admin) {
                    $where2 = ["account" => $admin["username"]];
                    $hospital = Hospital::field("id")->where(["status" => 1])
                        ->where('FIND_IN_SET("' . $this->user["id"] . '", admin_ids)')->find();
                     if($hospital) $where1=" hospital_id = ".$hospital["id"];
                }
            }
            $list = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 查找没通知的消息
     * */
    public function notice(){
        $user=$this->auth->getUserInfo();
        $hospital = Hospital::field("id")->where(["status" => 1])
            ->where('FIND_IN_SET("' . $user["id"] . '", admin_ids)')->find();
        $count=0;
        if($hospital){
            $count=$this->model->where(["status"=>'0','type_data'=>'1','hospital_id'=>$hospital["id"]])->count();
            $this->model->where(["status"=>'0','type_data'=>'1','hospital_id'=>$hospital["id"]])->update(["status"=>"1"]);
        }
        $this->success('成功','',["last_count"=>$count]);
    }
}
